package com.oceanstar.controller;

import com.oceanstar.Pojo.*;
import com.oceanstar.service.ICheckInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*")
public class CheckInController {

    @Autowired
    private ICheckInService checkInService;

    /*
     *用户签到
     */
    @GetMapping("/sign")
    public Result getSign(@RequestHeader("token") String token) {
        try {
            CheckInRecord checkInRecord = checkInService.getCheckInRecord(token);
            CheckInResponseDTO response = checkInService.buildCheckInResponseDTO(checkInRecord);
            return Result.success(response);
        } catch (Exception e) {
            return Result.error("签到失败：" + e.getMessage());
        }
    }

    /*
     *用户查询总积分
     */
    @GetMapping("/sign/points")
    public Result getPoints(@RequestHeader("token") String token) {
        try {
            CheckInRecord checkInRecord = checkInService.getLatestCheckInRecord(token);
            PointsDTO response = checkInService.buildPointsDTO(checkInRecord);
            return Result.success(response);
        } catch (Exception e) {
            return Result.error("获取积分失败：" + e.getMessage());
        }
    }

    /*
     *用户签到记录查询
     */
    @GetMapping("/sign/records")
    public Result getSignRecords(@RequestHeader("token") String token) {
        try {
            PageInfo<SignRecord> response = checkInService.getSignRecords(token);
            return Result.success(response);
        } catch (Exception e) {
            return Result.error("查询签到记录失败：" + e.getMessage());
        }
    }
}